package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.POJO.CheckItem;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.Callable;


/**
 * 预约管理
 * 检查项服务
 */
/*加了事务注解之后,必须明确当前服务实现的是哪个服务接口*/
@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {


    @Autowired
    private CheckItemDao checkItemDao;

    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);

        Page<CheckItem> checkItems = checkItemDao.pageQuery(queryString);
        long total = checkItems.getTotal();
        List<CheckItem> rows = checkItems.getResult();

        PageResult pageResult = new PageResult(total,rows);
        return  pageResult;



    }

    @Override
    public void delete(Integer id) {

        System.out.println(id);
        Long count = checkItemDao.findById(id);
        System.out.println(count);
        if (count > 0 ){
           throw  new RuntimeException("当前检查项被引用,不能删除");
        }else {
            checkItemDao.delete(id);
        }

    }

    @Override
    public CheckItem findItemById(Integer id) {
        return checkItemDao.findItemById(id);
    }

    @Override
    public void update(CheckItem checkItem) {
        checkItemDao.update(checkItem);
    }

    @Override
    public List<CheckItem> findAll() {
        List<CheckItem> list = checkItemDao.findAll();
        return list;

    }
}
